package com.enation.app.javashop.core.payment.service.impl;

import com.enation.app.javashop.core.payment.PaymentErrorCode;
import com.enation.app.javashop.core.payment.model.dos.PaymentBillDO;
import com.enation.app.javashop.core.payment.model.enums.TradeType;
import com.enation.app.javashop.core.payment.service.PaymentBillManager;
import com.enation.app.javashop.core.trade.cart.model.dos.OrderPermission;
import com.enation.app.javashop.core.client.trade.OrderClient;
import com.enation.app.javashop.core.trade.sdk.model.OrderDetailDTO;
import com.enation.app.javashop.framework.database.DaoSupport;
import com.enation.app.javashop.framework.database.Page;
import com.enation.app.javashop.framework.exception.ServiceException;
import com.enation.app.javashop.framework.util.CurrencyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 支付帐单业务类
 * @author fk
 * @version v2.0
 * @since v7.0.0
 * 2018-04-16 17:28:07
 */
@Service
public class PaymentBillManagerImpl implements PaymentBillManager{

	@Autowired
	@Qualifier("tradeDaoSupport")
	private DaoSupport daoSupport;
	@Autowired
	private OrderClient orderClient;

	@Override
	public Page list(int page,int pageSize){

		String sql = "select * from es_payment_bill  ";
		Page  webPage = this.daoSupport.queryForPage(sql,page, pageSize ,PaymentBillDO.class );

		return webPage;
	}

	@Override
	@Transactional(value = "tradeTransactionManager", propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public	PaymentBillDO  add(PaymentBillDO	paymentBill)	{
		this.daoSupport.insert(paymentBill);

		return paymentBill;
	}

	@Override
	@Transactional(value = "tradeTransactionManager", propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void pay(String outTradeNo, String returnTradeNo, TradeType tradeType, double payPrice) {

		PaymentBillDO bill = this.getBillByOutTradeNo(outTradeNo);
		//更新流水中的动态
		bill.setReturnTradeNo(returnTradeNo);
		bill.setIsPay(1);
		this.daoSupport.update(bill,bill.getBillId());

		//交易支付
		if(tradeType.equals(TradeType.trade)){
			//修改订单交易号
			String sql = "update es_order set pay_order_no = ? where trade_sn = ? ";
			this.daoSupport.execute(sql,returnTradeNo, bill.getSn());

			//更新订单的支付状态
			List<OrderDetailDTO> orderList = orderClient.getOrderByTradeSn(bill.getSn());
			//判断交易的金额是否正确
			Double  totalPrice = 0d;
			for(OrderDetailDTO orderDetailDTO : orderList){
				totalPrice = CurrencyUtil.add(totalPrice,orderDetailDTO.getOrderPrice());
			}

			if(!totalPrice.equals(payPrice)){
				throw new ServiceException(PaymentErrorCode.E503.code(),"金额不一致");
			}

			for(OrderDetailDTO orderDetailDTO : orderList){
				orderClient.payOrder(orderDetailDTO.getSn(),orderDetailDTO.getNeedPayMoney(), OrderPermission.client.name());
			}
			return;
		}
		//订单支付
		String sql = "update es_order set pay_order_no = ? where sn = ? ";
		this.daoSupport.execute(sql,returnTradeNo, bill.getSn());

		OrderDetailDTO order = orderClient.getModel(bill.getSn());
		if(!order.getOrderPrice().equals(payPrice)){
			throw new ServiceException(PaymentErrorCode.E503.code(),"金额不一致");
		}
		orderClient.payOrder(order.getSn(),payPrice, OrderPermission.client.name());
	}

	@Override
	public PaymentBillDO getBillByOutTradeNo(String outTradeNo) {

		String sql  =  "select * from es_payment_bill where out_trade_no = ?";
		return this.daoSupport.queryForObject(sql,PaymentBillDO.class,outTradeNo);
	}

	@Override
	public PaymentBillDO getBillBySnAndTradeType(String sn, String tradeType) {

		String sql = "select * from es_payment_bill where sn = ? and trade_type = ? order by bill_id desc limit 0,1";

		return this.daoSupport.queryForObject(sql, PaymentBillDO.class, sn ,tradeType);
	}

	@Override
	public PaymentBillDO getBillByReturnTradeNo(String returnTradeNo) {

		String sql = "select * from es_payment_bill where return_trade_no = ? ";

		return this.daoSupport.queryForObject(sql, PaymentBillDO.class, returnTradeNo);
	}
}
